package com.st.controller.background;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.st.entity.Goods;
import com.st.entity.ProductCategory;
import com.st.service.IGoodsService;
import com.st.service.IProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("goods")
public class GoodsController {
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IProductCategoryService productCategoryService;

    @RequestMapping("query")
    public ModelAndView list(Goods goods) {
        goods.setGoodsStatus(1);
        //判断页码变量是否为空，如果为空就赋值1 表示第一页
        if (goods.getPageNum() == null) {
            goods.setPageNum(1);
        }
        //一个查5条
        goods.setPageSize(5);
        //分页查询，注意该方法会对后面的第一个查询做分页
        PageHelper.startPage(goods.getPageNum(), goods.getPageSize());
        //1.查询商品列表（分页查询+条件查询）
        List<Goods> list = goodsService.query(goods);
        //把查出来的列表放到pageInfo中
        //分页信息，pageNum-页码 pageSize-单页记录数 ，pages-总页数
        PageInfo pageInfo = new PageInfo(list);
        //2.查询分类列表 看一看有什么类
        List<ProductCategory> cs = productCategoryService.query(new ProductCategory());
        ModelAndView mv = new ModelAndView();
        mv.addObject("pageInfo", pageInfo);
        mv.addObject("cs", cs);
        mv.addObject("goo", goods);
        mv.setViewName("/background/goods");
        return mv;
    }

    @RequestMapping("list")
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView();
        List<ProductCategory> cs = productCategoryService.query(new ProductCategory());
        mv.addObject("cs", cs);
        mv.setViewName("/background/goods_add");
        return mv;
    }

    //上传文件
    @RequestMapping("upload")
    @ResponseBody
    public JSONObject upload(@RequestParam("file") MultipartFile imgFile) {
        //传过来的文件的名字
        String oldName = imgFile.getOriginalFilename();
        //给传过来的文件起个新名字
        String newName = System.currentTimeMillis() + oldName.substring(oldName.lastIndexOf("."));
        //传到那个文件夹中
        String path = "C:\\photo\\";
        try {
            // //把传过来的文件传到一个具体的文件夹中 这个方法需要一个File类型的参数
            imgFile.transferTo(new File(path + newName));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        JSONObject json = new JSONObject();
        json.put("code", 1);
        json.put("imgUrl", newName);
        return json;
    }

    @RequestMapping("add")
    @ResponseBody
    public String add(Goods goods) {
        int count = goodsService.add(goods);
        if (count == 1) {
            return "1";
        } else {
            return "0";
        }
    }

    //下架商品
    @RequestMapping("down")
    public String down(String ids) {
        goodsService.goodsDown(ids);
        //跳转到列表页
        return "redirect:query";
    }

    //上架商品
    @RequestMapping("above")
    public String above(String ids) {
        goodsService.goodsAbove(ids);
        //跳转到列表页
        return "redirect:downGoods";
    }

    @RequestMapping("toUpdate")
    public ModelAndView toUpdate(Goods goods) {
        ModelAndView mv = new ModelAndView();
        List<Goods> list = goodsService.query(goods);
        List<ProductCategory> cs = productCategoryService.query(new ProductCategory());
        mv.addObject("list", list.get(0));
        mv.addObject("cs", cs);
        mv.setViewName("/background/goods_update");
        return mv;
    }

    @RequestMapping("update")
    @ResponseBody
    public String update(Goods goods) {
        int count = goodsService.update(goods);
        if (count == 1) {
            return "1";
        } else {
            return "0";
        }
    }

    @RequestMapping("downGoods")
    public ModelAndView list1(Goods goods) {
        goods.setGoodsStatus(0);
        //判断页码变量是否为空，如果为空就赋值1 表示第一页
        if (goods.getPageNum() == null) {
            goods.setPageNum(1);
        }
        //一个查5条
        goods.setPageSize(5);
        //分页查询，注意该方法会对后面的第一个查询做分页
        PageHelper.startPage(goods.getPageNum(), goods.getPageSize());
        //1.查询商品列表（分页查询+条件查询）
        List<Goods> list = goodsService.query(goods);
        //把查出来的列表放到pageInfo中
        //分页信息，pageNum-页码 pageSize-单页记录数 ，pages-总页数
        PageInfo pageInfo = new PageInfo(list);
        //2.查询分类列表 看一看有什么类
        List<ProductCategory> cs = productCategoryService.query(new ProductCategory());
        ModelAndView mv = new ModelAndView();
        mv.addObject("pageInfo", pageInfo);
        mv.addObject("cs", cs);
        mv.addObject("goo", goods);
        mv.setViewName("/background/down_goods");
        return mv;
    }

    @RequestMapping("del")
    @ResponseBody
    public String del(Integer id) {
        int count = goodsService.del(id);
        if (count == 1) {
            return "1";
        } else return "0";

    }

}
